package com.qf.servlet;

import com.qf.pojo.*;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCategoryService;
import com.qf.service.DtsGoodsService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.servlet
 * @Description:
 * @Date 2022/10/17 20:14
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController {

    @Autowired
    DtsBrandService dtsBrandService;

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsCategoryService dtsCategoryService;


    /**
     * 列表查询
     */
    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",defaultValue = "20")Integer limit,
                       @RequestParam(value = "sort",defaultValue = "add_time")String sort,
                       @RequestParam(value = "order",defaultValue = "desc") String order,
                       String goodsSn,String name){
//        获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<DtsBrand> brands = null;
//        判断是否是品牌管理员
        if(subject.hasRole("品牌制造商")){
//            根据品牌制造商ID查找对应的品牌
            brands = dtsBrandService.findByAdminId(admin.getId());
        }

        Map<String,Object> data = goodsService.findByPage(page,limit,sort,order,goodsSn,name,brands);

        return ResponseUtil.ok(data);
    }

    /**
     * 新增回显
     */
    @GetMapping("/catAndBrand")
    public Object catAndBrand(){
        //查询分类列表
        List<CatVo> categoryList = dtsCategoryService.findList();
        //查询品牌列表
        List<Map<String,Object>> brandList =  dtsBrandService.findList();
        //封装结果集
        Map<String,Object> data = new HashMap<>();
        data.put("categoryList",categoryList);
        data.put("brandList",brandList);

        return ResponseUtil.ok(data);
    }

    /**
     * 商品新增操作
     */

    @PostMapping("/create")
    public Object insert(@RequestBody GoodsAllinone goodsAllinone){

        //校验数据是否完善(是否为空)
        Object result = checkGoodsAllinone(goodsAllinone);
        if(result != null){
            return result;
        }

        try {
            goodsService.insert(goodsAllinone);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(-1, e.getMessage());
        }


        return ResponseUtil.ok();

    }

    private Object checkGoodsAllinone(GoodsAllinone goodsAllinone) {
        DtsGoods goods = goodsAllinone.getGoods();
        if(goods == null || StringUtils.isEmpty(goods.getName()) || StringUtils.isEmpty(goods.getGoodsSn())){
            return ResponseUtil.fail(-1, "商品名称和编号不能为空");
        }

        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        if(attributes!= null && attributes.length > 0){
            for (DtsGoodsAttribute attribute : attributes) {
                if(StringUtils.isEmpty(attribute.getAttribute())){
                    return ResponseUtil.fail(-1, "商品属性名称不能为空");
                }
            }
        }
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();

        if(specifications!= null && specifications.length > 0){
            for (DtsGoodsSpecification specification : specifications) {
                if(StringUtils.isEmpty(specification.getSpecification())){
                    return ResponseUtil.fail(-1, "商品规格名称不能为空");
                }
            }
        }
        DtsGoodsProduct[] products = goodsAllinone.getProducts();

        if(products== null || products.length == 0){
            return ResponseUtil.fail(-1, "商品库存不能为空");
        }


        //判断 品牌商
        Integer brandId = goods.getBrandId();
        if(brandId != null){
            DtsBrand brand = dtsBrandService.findById(brandId);
            if(brand == null){
                return ResponseUtil.fail(-1, "商品品牌不存在");
            }
        }


        //判断分类

        Integer categoryId = goods.getCategoryId();
        if(categoryId != null){
            DtsCategory category = dtsCategoryService.findById(categoryId);
            if(category == null){
                return ResponseUtil.fail(-1, "商品分类不存在");
            }
        }
        return null;
    }

    /**
     * 商品编辑的回显
     */
    @GetMapping("/detail")
    public Object detail(@RequestParam("id") Integer goodsId){

        Map<String,Object> data = goodsService.detail(goodsId);
        return ResponseUtil.ok(data);
    }

    /**
     * 商品的更新保存
     */
    @PostMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){

        goodsService.update(goodsAllinone);

        return ResponseUtil.ok();
    }

    /**
     * 商品的删除
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGoods dtsGoods){

        goodsService.delete(dtsGoods);
        return ResponseUtil.ok();

    }
}
